package karaoke.servlet.Local;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import karaoke.modelo.Local;
import karaoke.negocio.GestionLocales;
import karaoke.excepcion.DAOExcepcion;

/**
 * Servlet implementation class LocalEditarServlet
 */
@WebServlet("/LocalEditarServlet")
public class LocalEditarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LocalEditarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String ab = request.getParameter("id");
		int a = Integer.parseInt(ab);
		
		GestionLocales negocio = new GestionLocales();
		try {
			Local vo = negocio.obtener(a);
			
			
			request.setAttribute("vo", vo);
		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		RequestDispatcher rd = request.getRequestDispatcher("Mantenimiento_Locales_Editar.jsp");
		rd.forward(request, response);
	}
		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		// ACTUALIZAR LA TABLA
		
		String id = request.getParameter("id");
		String nombre = request.getParameter("txtNombre");
		String direccion = request.getParameter("txtDireccion");

		String tel = request.getParameter("txtTelefono");
		int telefono = Integer.parseInt(tel);

		String correo = request.getParameter("txtCorreo");
		String descripcion = request.getParameter("txtDescripcion");
		String admin = request.getParameter("txtAdmin");

		GestionLocales local = new GestionLocales();
		try {
			Local vo = new Local();
			vo.setLocal_Id(Integer.parseInt(id));
			vo.setLocal_Nombre(nombre);
			vo.setLocal_Direccion(direccion);
			vo.setLocal_Telefono(telefono);
			vo.setLocal_Correo(correo);
			vo.setLocal_Descripcion(descripcion);
			vo.setLocal_Admin(admin);
			
			local.actualizar(vo);
			response.sendRedirect(request.getContextPath() + "/Mantenimiento_Locales_Buscar.jsp");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}

		
		
	}

}
